#!/bin/bash
# export JAVA_HOME="" # Set here if not set in environment
export JAVA_OPTS="-Xmx12G -Xms128M" # Set as much memory as possible
JCUDA_VERSION="0.5.5" # Fix if needed
BIDMACH_ROOT="${BASH_SOURCE[0]}"
if [ ! `uname` = "Darwin" ]; then
  BIDMACH_ROOT=`readlink -f "${BIDMACH_ROOT}"`
else 
  BIDMACH_ROOT=`readlink "${BIDMACH_ROOT}"`
fi
BIDMACH_ROOT=`dirname "$BIDMACH_ROOT"`
BIDMACH_ROOT="$( echo ${BIDMACH_ROOT} | sed s+/cygdrive/c+c:+ )" 
JCUDA_LIBDIR="${BIDMACH_ROOT}/lib"
LIBDIR="${BIDMACH_ROOT}/lib"
if [ `uname` = "Darwin" ]; then
  export DYLD_LIBRARY_PATH="${LIBDIR}:${LIBDIR}/cuda:${DYLD_LIBRARY_PATH}"
else
  export LD_LIBRARY_PATH="${LIBDIR}:${LIBDIR}/cuda:${LD_LIBRARY_PATH}" 
fi

BIDMACH_LIBS="${LIBDIR}/BIDMat.jar;${LIBDIR}/ptplot.jar;${LIBDIR}/ptplotapplication.jar;${LIBDIR}/jhdf5.jar;${LIBDIR}/commons-math3-3.1.1.jar;${LIBDIR}/lz4-1.1.2.jar"
JCUDA_LIBS="${JCUDA_LIBDIR}/jcuda-${JCUDA_VERSION}.jar;${JCUDA_LIBDIR}/jcublas-${JCUDA_VERSION}.jar;${JCUDA_LIBDIR}/jcufft-${JCUDA_VERSION}.jar;${JCUDA_LIBDIR}/jcurand-${JCUDA_VERSION}.jar;${JCUDA_LIBDIR}/jcusparse-${JCUDA_VERSION}.jar"

export ALL_LIBS="${BIDMACH_ROOT}/BIDMach.jar;${BIDMACH_LIBS};${JCUDA_LIBS};${JAVA_HOME}/lib/tools.jar"

if [ ! "$OS" = "Windows_NT" ]; then
    export ALL_LIBS=`echo "${ALL_LIBS}" | sed 's/;/:/g'`
else
    export JAVA_OPTS="-Djava.library.path=${LIBDIR};${LIBDIR}\\cuda "$JAVA_OPTS
fi

${BIDMACH_ROOT}/scala/bin/scala -nobootcp -cp "${ALL_LIBS}" -Yrepl-sync -i ${LIBDIR}/bidmach_init.scala "$@"
